<?php
include '../../common.inc.php';
$hd = new ImportLoop();
$hd->setAim('mysql:host=192.168.1.19;dbname=caiji', 'root', 'root');
$hd->setSource('mysql:host=192.168.1.19;dbname=ixdcw', 'root', 'root');
$hd->setSourceInfo('xd_auto_category', 'arid', 'typename="rank" and level=2');
$params = array(
	'maxid'     => isset($_GET['maxid']) ? $_GET['maxid'] : 0,
	'id'        => isset($_GET['id']) && !empty($_GET['id']) ? $_GET['id'] : 0,
	'startTime' => isset($_GET['startTime']) ? $_GET['startTime'] : time(),
	'limit'     => 20,
	'interval'  => 1,
	'debug'     => isset($_GET['debug']) ? $_GET['debug'] : 'xddl',
);
$hd->initParams($params);

$result = $hd->getDbSource()->query('select * from ' . $hd->getSourceTable() . ' where typename="rank" and level=2 and arid>' . $hd->getParam('id') . ' order by arid ASC limit ' . $hd->getParam('limit'));
$rs     = $result->fetchAll();

if (!$rs) {
	$hd->end();
} else {
	$carFolders = array();
	foreach ($rs as $k => $v) {
		$hd->setParam('id', $v['arid']);
		$carFolder    = AutoImport::carFolderByInfo($v);
		$carFolders[] = array($carFolder . '/公共属性', $v['parentid'], $v['arid'], 0);
		$models       = AutoModel::shortByCarid($v['arid']);
		if (empty($models)) {
			continue;
		} else {
			foreach ($models as $model) {
				$carFolders[] = array($carFolder . '/' . $model['name'], AutoCar::getBrandId($v['arid']), $v['arid'], $model['amid']);
			}
		}

	}
	$sql = "insert import_picture (folder, arbid, arcid, amid) values";
	$values = array();
	foreach($carFolders as $k => $va) {
		$values[] = '(\''.implode("','", $va).'\')';
	}
	$sql .= implode(',', $values);
	if ($hd->getDbAim()->query($sql)) {
		var_dump($v['arid'], $v['name'], $hd->times(), $hd->getParam('maxid'));
	} else {
		print_r($sql);
		exit();
	}
}
var_dump($hd->getParams());
echo $hd->start($hd->getParams());